package io.chenglicun.modules.clc.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.entity.BaseEntity;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.utils.ConvertUtils;
import io.chenglicun.modules.clc.dao.ClcQuestionDao;
import io.chenglicun.modules.clc.dto.ClcQuestionAnswerDTO;
import io.chenglicun.modules.clc.dto.ClcQuestionDTO;
import io.chenglicun.modules.clc.entity.ClcQuestionEntity;
import io.chenglicun.modules.clc.service.ClcQuestionAnswerService;
import io.chenglicun.modules.clc.service.ClcQuestionService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 题目管理
 *
 * @author HB 137664951@qq.com
 * @since 1.0 2023-10-18
 */
@Service
public class ClcQuestionServiceImpl extends CrudServiceImpl<ClcQuestionDao, ClcQuestionEntity, ClcQuestionDTO> implements ClcQuestionService {

    @Autowired
    private ClcQuestionAnswerService questionAnswerService;


    @Override
    public QueryWrapper<ClcQuestionEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ClcQuestionEntity> wrapper = new QueryWrapper<>();

        Object bankId = params.get("bankId");
        wrapper.eq(ObjectUtil.isNotEmpty(bankId), "bank_id", bankId);

        return wrapper;
    }

    @Override
    public ClcQuestionDTO get(Long id) {
        ClcQuestionDTO dto = super.get(id);

        List<ClcQuestionAnswerDTO> list = questionAnswerService.getListByQuestionId(dto.getId());
        dto.setQuestionAnswerList(list);

        return dto;
    }

    @Override
    public List<ClcQuestionDTO> getByIds(List<Long> ids) {
        List<ClcQuestionEntity> entities = baseDao.selectList(
                new QueryWrapper<ClcQuestionEntity>().lambda()
                        .in(BaseEntity::getId, ids)
        );
        List<ClcQuestionDTO> dtos = ConvertUtils.sourceToTarget(entities, ClcQuestionDTO.class);

        List<ClcQuestionAnswerDTO> list = questionAnswerService.getListByQuestionId(entities.stream().map(BaseEntity::getId).toArray(Long[]::new));
        for (ClcQuestionDTO dto : dtos) {
            List<ClcQuestionAnswerDTO> collect = list.stream().filter(t -> t.getQuestionId().equals(dto.getId())).collect(Collectors.toList());
            dto.setQuestionAnswerList(collect);
        }

        return dtos;
    }

    @Override
    public List<ClcQuestionDTO> getByBankIds(Long... bankIds) {
        List<ClcQuestionEntity> entities = baseDao.selectList(
                new QueryWrapper<ClcQuestionEntity>().lambda()
                        .in(ClcQuestionEntity::getBankId, Arrays.asList(bankIds))
        );
        return ConvertUtils.sourceToTarget(entities, ClcQuestionDTO.class);
    }

    @Override
    @Transactional
    public void save(ClcQuestionDTO dto) {
        super.save(dto);

        List<ClcQuestionAnswerDTO> list = dto.getQuestionAnswerList();
        list.forEach(t -> t.setQuestionId(dto.getId()));
        questionAnswerService.saveBatch(list);
    }

    @Override
    @Transactional
    public void update(ClcQuestionDTO dto) {
        super.update(dto);

        questionAnswerService.deleteByQuestionIds(dto.getId());

        List<ClcQuestionAnswerDTO> list = dto.getQuestionAnswerList();
        list.forEach(t -> t.setQuestionId(dto.getId()));
        questionAnswerService.saveBatch(list);
    }

    @Override
    @Transactional
    public void delete(Long[] ids) {
        super.delete(ids);

        questionAnswerService.deleteByQuestionIds(ids);
    }
}